//
//  YZLCarDetailViewController.m
//  Car
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLCarDetailViewController.h"
#import "HTTPTOOL.h"
#import <UIImageView+WebCache.h>
//综合信息
#import "YZLCarDetailModel.h"
#import "YZLCarDetailTableViewCell.h"
#import "YZLCarTypeMessageViewController.h"
//图片信息
#import "YZLCarImageModel.h"
#import "YZLCarImageCollectionViewCell.h"
#import "YZLCarImageViewController.h"
#import "YZLCarImageListViewController.h"
#import "YZLCarButtonCollectionViewCell.h"
//论坛
#import "MJRefresh.h"
#import "YZLCarForumModel.h"
#import "YZLCarForumTableViewCell.h"
#import "YZLCarForumDetailViewController.h"
//资讯
#import "YZLCarMessageModel.h"
#import "YZLCarMessageTableViewCell.h"
#import "YZLCarMessageDetailViewController.h"
//收藏
#import "YZLCollectionModel.h"

@interface YZLCarDetailViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
//综合信息
@property (nonatomic, strong) UISegmentedControl *segment;
@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, copy) NSString *seriesName;
@property (nonatomic, copy) NSString *price;
@property (nonatomic, copy) NSString *image;
@property (nonatomic, copy) NSString *clubId;
@property (nonatomic, strong) NSMutableArray *modelArr;
@property (nonatomic, strong) UIView *activity;
@property (nonatomic, strong) UIActivityIndicatorView *activityView;
//图片信息
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *imageCategoryArr;
@property (nonatomic, strong) NSMutableArray *imageModelArr;
@property (nonatomic, strong) UIView *typeView;
@property (nonatomic, copy) NSString *priceStr;
@property (nonatomic, copy) NSString *dropPriceStr;
@property (nonatomic, strong) UICollectionReusableView *customView; //CollectionView上加headerView
@property (nonatomic, assign) NSInteger indexPathNum;
//论坛 (forum)
@property (nonatomic, strong) UITableView *forumTableView;
@property (nonatomic, assign) NSInteger startNum;//记录start的值
@property (nonatomic, assign) BOOL UpLoading;//标记上拉操作还是下拉操作，yes为上拉
@property (nonatomic, strong) NSMutableArray *forumModelArr;
@property (nonatomic, strong) NSMutableArray *postLinkArr;

//资讯 (message)
@property (nonatomic, strong) UITableView *messageTableView;
@property (nonatomic, strong) NSMutableArray *messageModelArr;
@property (nonatomic, strong) NSMutableArray *newsLinkArr;
//收藏
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIAlertController *alert;
@property (nonatomic, strong) NSMutableArray *fileArr;
@property (nonatomic, strong) NSMutableArray *idArr;
@property (nonatomic, assign) BOOL iscollect;


@end 

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@implementation YZLCarDetailViewController
#pragma mark - 综合信息和图片信息的数据处理
- (void)dataHandle {
    //综合信息
    NSString *str = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcarapp/getSeriesInfoNew.php?cityId=64&seriesId=%@&version=6.2.3", self.seriesId];
    [HTTPTOOL GETWithURL:str body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        //综合信息
        self.seriesName = [result objectForKey:@"seriesName"];
        self.price = [result objectForKey:@"price"];
        self.image = [result objectForKey:@"image"];
        self.clubId = [NSString stringWithFormat:@"%@", [result objectForKey:@"clubId"]];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 270)];
        imageView.backgroundColor = [UIColor lightGrayColor];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.image] placeholderImage:[UIImage imageNamed:@"D0"]];
        [self.headerView addSubview:imageView];
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 280, WIDTH - 20, 20)];
        nameLabel.text = self.seriesName;
        nameLabel.textAlignment = 1;
        [self.headerView addSubview:nameLabel];
        
        UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 320, WIDTH - 20, 20)];
        NSString *str = @"厂商指导价: ";
        priceLabel.text = [str stringByAppendingString:self.price];
        priceLabel.textAlignment = 1;
        [self.headerView addSubview:priceLabel];
        
        //图片信息
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, 30)];
        [self.scroll addSubview:label];
        label.text = self.seriesName;
        label.textAlignment = 1;
        label.backgroundColor = [UIColor colorWithWhite:0.970 alpha:1.000];
        
        NSArray *arr = [result objectForKey:@"saleSubSeries"];
        self.modelArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            YZLCarDetailModel *model = [YZLCarDetailModel CarDetailModelWithDic:dic];
            [self.modelArr addObject:model];
        }
        
        [self.tableView reloadData];
        self.activity.hidden = YES;
        [self.activityView stopAnimating];
        
    } fail:^(NSError *error) {
        
    }];
    
    //图片信息
    NSString *str2 = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcarapp/getImageSummaryNew.php?cityId=64&id=%@&type=1", self.seriesId];
    [HTTPTOOL GETWithURL:str2 body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        NSArray *arr = [result objectForKey:@"categoryList"];
        self.priceStr = [result objectForKey:@"price"];
        self.dropPriceStr = [result objectForKey:@"dropPrice"];
        //类型
        self.imageCategoryArr = [NSMutableArray array];
        self.imageModelArr = [NSMutableArray array];
                for (NSDictionary *dic in arr) {
            
            [self.imageCategoryArr addObject:[dic objectForKey:@"imageCategory"]];
            YZLCarImageModel *model = [YZLCarImageModel CarImageModelWithDic:dic];
            [self.imageModelArr addObject:model];
            
        }
        
        [self.collectionView reloadData];
        
        if (self.imageModelArr.count == 0) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(WIDTH, 30, WIDTH, self.scroll.frame.size.height)];
            [self.scroll addSubview:view];
            view.backgroundColor = [UIColor whiteColor];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((WIDTH - 200) / 2.f, 80, 200, 20)];
            label.text = @"暂无图片!";
            label.textAlignment = 1;
            label.textColor = [UIColor colorWithWhite:0.8 alpha:1.000];
            [view addSubview:label];
        }
        
    } fail:^(NSError *error) {
        
    }];
}

#pragma mark - 论坛和资讯的数据处理
- (void)dataHandleWithForumAndMessage:(NSInteger )startNum {
    
    //论坛
    NSString *str1 = [NSString stringWithFormat:@"http://mi.xcar.com.cn/interface/xcarapp/getPostList.php?forumId=%@&limit=20&offset=%ld&type=0&ver=6.2.3", self.clubId, self.startNum];
    [HTTPTOOL GETWithURL:str1 body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        self.startNum += 20;
        //下拉刷新 清空数组
        if (self.UpLoading == NO) {
//            [self.forumModelArr removeAllObjects];
        }
        NSArray *arr = [result objectForKey:@"postList"];
        for (NSDictionary *dic in arr) {
            YZLCarForumModel *model = [YZLCarForumModel CarForumModelWithDic:dic];
            [self.forumModelArr addObject:model];
            [self.postLinkArr addObject:[dic objectForKey:@"postLink"]];
            
        }
        
        [self.forumTableView headerEndRefreshing];
        [self.forumTableView footerEndRefreshing];
        [self.forumTableView reloadData];
        
        if (self.forumModelArr.count == 0) {
            
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(WIDTH * 2, 0, WIDTH, self.scroll.frame.size.height)];
            [self.scroll addSubview:view];
            view.backgroundColor = [UIColor whiteColor];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((WIDTH - 200) / 2.f, 80, 200, 20)];
            label.text = @"暂无论坛!";
            label.textAlignment = 1;
            label.textColor = [UIColor colorWithWhite:0.8 alpha:1.000];
            [view addSubview:label];
        }
        
    } fail:^(NSError *error) {
        
    }];
    
    //资讯
    NSString *str2 = [NSString stringWithFormat:@"http://a.xcar.com.cn/interface/6.0/getNewsListBySeriesId.php?limit=20&offset=%ld&seriesId=%@&type=1", self.startNum, self.seriesId];
    [HTTPTOOL GETWithURL:str2 body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        self.startNum += 20;
        for (NSDictionary *dic in result) {
            YZLCarMessageModel *model = [YZLCarMessageModel CarMessageModelWithDic:dic];
            [self.messageModelArr addObject:model];
            [self.newsLinkArr addObject:[dic objectForKey:@"newsLink"]];
        }
        
        [self.messageTableView headerEndRefreshing];
        [self.messageTableView footerEndRefreshing];
        [self.messageTableView reloadData];
        
        if (self.messageModelArr.count == 0) {
            
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(WIDTH * 3, 0, WIDTH, self.scroll.frame.size.height)];
            [self.scroll addSubview:view];
            view.backgroundColor = [UIColor whiteColor];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((WIDTH - 200) / 2.f, 80, 200, 20)];
            label.text = @"暂无资讯!";
            label.textAlignment = 1;
            label.textColor = [UIColor colorWithWhite:0.8 alpha:1.000];
            [view addSubview:label];
        }

        
    } fail:^(NSError *error) {
        
    }];
}

#pragma mark - 上拉下拉刷新
//下拉刷新
- (void)addHeader
{
    __block YZLCarDetailViewController *downVC = self;
    // 添加下拉刷新头部控件
    [self.forumTableView addHeaderWithCallback:^{
        downVC.UpLoading = NO;
        //重新请求数据
        [downVC dataHandleWithForumAndMessage:0];
    }];
    [self.messageTableView addHeaderWithCallback:^{
        downVC.UpLoading = NO;
        [downVC dataHandleWithForumAndMessage:0];
    }];
}
//上拉加载
- (void)addFooter
{
    __block YZLCarDetailViewController *upVC = self;
    //防止内存问题
    __weak YZLCarDetailViewController *weakSelf = self;
    // 添加上拉刷新尾部控件
    [self.forumTableView addFooterWithCallback:^{
        upVC.UpLoading = YES;
        //请求更多数据
        [upVC dataHandleWithForumAndMessage:weakSelf.startNum];
    }];
    [self.messageTableView addFooterWithCallback:^{
        upVC.UpLoading = YES;
        [upVC dataHandleWithForumAndMessage:weakSelf.startNum];
    }];
}


//综合信息
#pragma mark - 创建scrollView
- (void)createScrollView {
    
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, WIDTH, HEIGHT - 64)];
    [self.view addSubview:self.scroll];
    self.scroll.contentSize = CGSizeMake(WIDTH * 4, 0);
    self.scroll.pagingEnabled = YES;
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.delegate = self;
}

#pragma mark - 创建tableView
- (void)createTableView {
    
    self.segment = [[UISegmentedControl alloc] initWithItems:@[@"综合", @"图片", @"论坛", @"资讯"]];
    self.segment.frame = CGRectMake(0, 64, WIDTH, 40);
    [self.view addSubview:self.segment];
    self.segment.tintColor = [UIColor whiteColor];
    self.segment.backgroundColor = [UIColor whiteColor];
    self.segment.selectedSegmentIndex = 0;
    
    //segment 分隔线
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 104, WIDTH, 0.5)];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor lightGrayColor];
    
    /* 改变字体大小 */
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],NSFontAttributeName,  [UIColor blackColor], NSForegroundColorAttributeName,nil];
    [self.segment setTitleTextAttributes:dic forState:UIControlStateNormal];
    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:17],NSFontAttributeName, [UIColor redColor], NSForegroundColorAttributeName,nil];
    [self.segment setTitleTextAttributes:dic1 forState:UIControlStateSelected];
    [self.segment addTarget:self action:@selector(segment:) forControlEvents:UIControlEventValueChanged];
    
    //综合信息
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 95) style:UITableViewStylePlain];
    [self.scroll addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 90;
    //去除分隔线
    UIView *view1 = [[UIView alloc] init];
    self.tableView.tableFooterView = view1;
    
    //论坛 (forum)
    self.forumTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH * 2, 10, WIDTH, HEIGHT - 115) style:UITableViewStylePlain];
    [self.scroll addSubview:self.forumTableView];
    self.forumTableView.delegate = self;
    self.forumTableView.dataSource = self;
    self.forumTableView.rowHeight = 70;
    //资讯 (message)
    self.messageTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH * 3, 5, WIDTH, HEIGHT - 115) style:UITableViewStylePlain];
    [self.scroll addSubview:self.messageTableView];
    self.messageTableView.delegate = self;
    self.messageTableView.dataSource = self;
    self.messageTableView.rowHeight = 70;
}

//segment点击方法
- (void)segment:(UISegmentedControl *)segment {
    
    switch (segment.selectedSegmentIndex) {
        case 0:
            self.scroll.contentOffset = CGPointMake(0, 0);
            break;
        case 1:
            self.scroll.contentOffset = CGPointMake(WIDTH, 0);
            break;
        case 2:
            self.scroll.contentOffset = CGPointMake(WIDTH * 2, 0);
            //自动刷新
            [self.forumTableView headerBeginRefreshing];
            break;
        case 3:
            self.scroll.contentOffset = CGPointMake(WIDTH * 3, 0);
            [self.messageTableView headerBeginRefreshing];

            break;
        default:
            break;
    }
    
}

//scrollView 滑动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (scrollView == self.scroll) {
        
        if (scrollView.contentOffset.x == 0) {
            self.segment.selectedSegmentIndex = 0;
        }
        if (scrollView.contentOffset.x == WIDTH) {
            self.segment.selectedSegmentIndex = 1;
        }
        if (scrollView.contentOffset.x == WIDTH * 2) {
            self.segment.selectedSegmentIndex = 2;
            //自动刷新
            [self.forumTableView headerBeginRefreshing];

        }
        if (scrollView.contentOffset.x == WIDTH * 3) {
            self.segment.selectedSegmentIndex = 3;
            [self.messageTableView headerBeginRefreshing];
        }
    }
}

//图片信息
#pragma mark - 创建collectionView
- (void)createCollectionView {
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake((WIDTH - 41) / 3.f, 90);
    flow.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(WIDTH, 30, WIDTH, self.scroll.frame.size.height - 70) collectionViewLayout:flow];
    [self.scroll addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerClass:[YZLCarImageCollectionViewCell class] forCellWithReuseIdentifier:@"image"];
    [self.collectionView registerClass:[YZLCarButtonCollectionViewCell class] forCellWithReuseIdentifier:@"button"];
    //注册header
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"header"];
}

#pragma mark - 显示navigationbar
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark -
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"车系信息";
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self createScrollView];
    [self createTableView];
    [self dataHandle];
    [self createCollectionView];
    [self addHeader];
    [self addFooter];
    
    //bar颜色
    [self navigationbarColor];
    
    //返回    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:(UIBarButtonItemStylePlain) target:self action:@selector(lbutton)];

//    NSLog(@"%@", self.navigationItem.leftBarButtonItem.target);
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 350)];
    self.tableView.tableHeaderView = self.headerView;
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 310, WIDTH - 20, 0.5)];
    view1.backgroundColor = [UIColor grayColor];
    [self.headerView addSubview:view1];
    
    //收藏
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(collectionButton)];
    
    //收藏的数组初始化
    self.fileArr = [NSMutableArray array];
    self.idArr = [NSMutableArray array];
    
    /* 指示器 */
    self.activity = [[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64)];
    self.activity.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.activity];
    self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityView.color = [UIColor lightGrayColor];
    self.activityView.center = CGPointMake(self.activity.frame.size.width / 2.f, self.activity.frame.size.height / 2.f - 64);
    [self.activity addSubview:self.activityView];
    [self.activityView startAnimating];
    
    //论坛
    self.forumModelArr = [NSMutableArray array];
    self.postLinkArr = [NSMutableArray array];
    
    //资讯
    self.messageModelArr = [NSMutableArray array];
    self.newsLinkArr = [NSMutableArray array];
}

//返回
- (void)lbutton {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

#pragma mark - 收藏
- (void)collectionButton {
    
    //初始收藏的Model
    YZLCollectionModel *CollectionModel = [[YZLCollectionModel alloc] init];
    CollectionModel.nID = self.seriesId;
    CollectionModel.title = self.seriesName;
    CollectionModel.image = self.image;
    CollectionModel.price = self.price;
    
    /* 文件夹路径 */
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSLog(@"%@", path);
    
    /* 创建文件夹管理器 */
    NSFileManager *manager = [NSFileManager defaultManager];
    /* 创建文件夹 */
    NSString *filePath = [path stringByAppendingPathComponent:@"收藏"];
    [manager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *strPath = [filePath stringByAppendingPathComponent:@"Car.plist"];
    /* 判断是否存在strPath文件夹 */
    if (![manager fileExistsAtPath:strPath]) {
        [self.fileArr addObject:CollectionModel];
        /* 将数组写入本地 */
        [NSKeyedArchiver archiveRootObject:self.fileArr toFile:strPath];
        //提示框
        self.alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功!" preferredStyle:UIAlertControllerStyleAlert];
        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dismissAlertView) userInfo:nil repeats:NO];
        [self presentViewController:self.alert animated:YES completion:nil];
        
    }else {
        /* 将数组反归档 取出数组 */
        self.fileArr = [NSKeyedUnarchiver unarchiveObjectWithFile:strPath];
        for (YZLCollectionModel *Nmodel in self.fileArr) {
            [self.idArr addObject:Nmodel.nID];
        }
        /* 判断数组是否包含将要收藏的Model */
        if (![self.idArr containsObject:CollectionModel.nID]) {
            [self.fileArr addObject:CollectionModel];
            /* 将数组写入本地归档 */
            [NSKeyedArchiver archiveRootObject:self.fileArr toFile:strPath];
            //提示框
            self.alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功!" preferredStyle:UIAlertControllerStyleAlert];
            [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dismissAlertView) userInfo:nil repeats:NO];
            [self presentViewController:self.alert animated:YES completion:nil];

        } else {
            //提示框
            self.alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已收藏!" preferredStyle:UIAlertControllerStyleAlert];
            [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dismissAlertView) userInfo:nil repeats:NO];
            [self presentViewController:self.alert animated:YES completion:nil];

        }        
    }
}

//实现alert自动消失方法
- (void)dismissAlertView {
    
    [self.alert dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - tableView 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == self.tableView) {
        
        return self.modelArr.count;
    }else {
        
        return 1;
    }
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (tableView == self.tableView) {
        
        YZLCarDetailModel *model = [self.modelArr objectAtIndex:section];
        NSString *str = model.engine;
        return str;
    }else {
        
        return nil;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.tableView) {
        
        YZLCarDetailModel *model = [self.modelArr objectAtIndex:section];
        NSArray *arr = model.cars;
        return arr.count;
        
    }else if (tableView == self.forumTableView) {
        
        return self.forumModelArr.count;
        
    }else {
        
        return self.messageModelArr.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.tableView) {
        
        YZLCarDetailModel *model = [self.modelArr objectAtIndex:indexPath.section];
        YZLCarDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[YZLCarDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.num = indexPath.row;
        cell.model = model;
        return cell;
        
    }else if (tableView == self.forumTableView) {
        
        YZLCarForumModel *model = [self.forumModelArr objectAtIndex:indexPath.row];
        YZLCarForumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        if (cell == nil) {
            cell = [[YZLCarForumTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
        }
        cell.model = model;
        return cell;
    }else {
        
        YZLCarMessageModel *model = [self.messageModelArr objectAtIndex:indexPath.row];
        YZLCarMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        if (cell == nil) {
            cell = [[YZLCarMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
        }
        
        cell.model = model;
        return cell;
    }
}

#pragma mark - 点击tableViewCell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //取消阴影效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (tableView == self.tableView) {
        
        YZLCarTypeMessageViewController *vc = [[YZLCarTypeMessageViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        YZLCarDetailModel *model = [self.modelArr objectAtIndex:indexPath.section];
        NSString *carID = [NSString stringWithFormat:@"%@", [[model.cars objectAtIndex:indexPath.row] objectForKey:@"carId"]];
        vc.carId = carID;
    }else if (tableView == self.forumTableView) {
        
        YZLCarForumDetailViewController *vc = [[YZLCarForumDetailViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        vc.postLinkStr = [self.postLinkArr objectAtIndex:indexPath.row];
        
    }else {
        
        YZLCarMessageDetailViewController *vc = [[YZLCarMessageDetailViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        vc.newsLinkStr = [self.newsLinkArr objectAtIndex:indexPath.row];
    }
    
    
}

#pragma mark - collectionView 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    YZLCarImageModel *model = [self.imageModelArr objectAtIndex:section];
    NSArray *arr = model.imagesList;
    if (arr.count == 5) {
        
        return arr.count + 1;
        
    }else {
        
        return arr.count;
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return self.imageCategoryArr.count;
}

//collectionView 加header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{
    
    self.customView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    self.customView.backgroundColor = [UIColor whiteColor];
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 5.0, 100, 20)];
    headerLabel.backgroundColor = [UIColor whiteColor];
    headerLabel.textColor = [UIColor colorWithWhite:0.200 alpha:1.000];
    headerLabel.font = [UIFont boldSystemFontOfSize:15];
    headerLabel.text = [self.imageCategoryArr objectAtIndex:indexPath.section];
        [self.customView addSubview:headerLabel];
    return self.customView;
    
}

//header 大小
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGSize size = {WIDTH, 30};
    return size;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    YZLCarImageModel *model = [self.imageModelArr objectAtIndex:indexPath.section];
    NSArray *arr = model.imagesList;
    
    if (arr.count == 5) {
        
        if (indexPath.item < 5) {
            
            YZLCarImageModel *model = [self.imageModelArr objectAtIndex:indexPath.section];
            YZLCarImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"image" forIndexPath:indexPath];
            cell.num = indexPath.item;
            cell.imageModel = model;
            return cell;
        }else {
            
            YZLCarButtonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"button" forIndexPath:indexPath];
            return cell;
        }
    }else {
        
        YZLCarImageModel *model = [self.imageModelArr objectAtIndex:indexPath.section];
        YZLCarImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"image" forIndexPath:indexPath];
        cell.num = indexPath.item;
        cell.imageModel = model;
        return cell;
        
    }
}

#pragma mark - 点击Item跳转
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item < 5) {
        
        YZLCarImageViewController *vc = [[YZLCarImageViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        vc.titleStr = [self.imageCategoryArr objectAtIndex:indexPath.section];
        YZLCarImageModel *model = [self.imageModelArr objectAtIndex:indexPath.section];
        vc.imageArr = model.imagesList;
        vc.itemNum = indexPath.item;
        vc.price = self.priceStr;
        vc.dropPrice = self.dropPriceStr;
    }else {
        
        YZLCarImageListViewController *VC = [[YZLCarImageListViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
        VC.sericeStr = self.seriesId;
        VC.headStr = [self.imageCategoryArr objectAtIndex:indexPath.section];
        VC.typeStr = [NSString stringWithFormat:@"%ld", indexPath.section + 1];
        
    }
    
}

#pragma mark - 隐藏tabBar
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
